package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.QuanZiMqService;
import com.tanhua.app.service.QuanziService;
import com.tanhua.domain.mongo.Publish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class QuanziController {

    @Autowired
    private QuanziService quanziService;

    @PostMapping(value = "/movements",name = "发布动态")
    public ResponseEntity toPublish(Publish publish, MultipartFile[] imageContent) throws ClientException, IOException {
        return quanziService.toPublish(publish,imageContent);
    }

    @GetMapping(value = "/movements/all",name = "我的动态")
    public ResponseEntity findMyPublish(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize, Long userId) throws ClientException, IOException {

        ResponseEntity aaa = quanziService.findMyPublish(page, pagesize, userId);
        System.out.println(aaa);
        return aaa;
    }

    @GetMapping(value = "/movements",name = "我的好友动态")
    public ResponseEntity findTimeLinePublish(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) throws ClientException, IOException {
        ResponseEntity aaa = quanziService.findTimeLinePublish(page, pagesize);
        return aaa;
    }




    @GetMapping(value = "/comments",name = "查询评论列表动态")
    public ResponseEntity findplList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize,String movementId) throws ClientException, IOException {
        return quanziService.findplList(page,pagesize,movementId);
    }





    @Autowired
    private QuanZiMqService quanZiMqService;


    /*当前登录用户每次执行的历史记录*/
    @GetMapping(value = "/movements/{id}",name = "查询单条动态")
    public ResponseEntity findByPublish(@PathVariable("id") String publishId) throws ClientException, IOException {

        // 往消息中间件中存数据 （type=1）
        quanZiMqService.sendMsg(publishId,1);
        return quanziService.findByPublish(publishId);
    }

    @GetMapping(value = "/movements/{id}/like",name = "点赞")
    public ResponseEntity likePublish(@PathVariable("id") String publishId) throws ClientException, IOException {
        // 往消息中间件中存数据 （type=2）
        quanZiMqService.sendMsg(publishId,2);
        return quanziService.likePublish(publishId);
    }

    @GetMapping(value = "/movements/{id}/love",name = "喜欢")
    public ResponseEntity lovePublish(@PathVariable("id") String publishId) throws ClientException, IOException {
        // 往消息中间件中存数据 （type=3）
        quanZiMqService.sendMsg(publishId,3);
        return quanziService.lovePublish(publishId);
    }

    @PostMapping(value = "/comments",name = "评论提交")
    public ResponseEntity todpl(@RequestBody Map param)  throws ClientException, IOException {
        String publishId = (String) param.get("movementId");
        String text = (String) param.get("comment");
        // 往消息中间件中存数据 （type=4）
        quanZiMqService.sendMsg(publishId,4);
        return quanziService.todpl(publishId,text);
    }

    @GetMapping(value = "/movements/{id}/dislike",name = "删除点赞")
    public ResponseEntity removeLikePublish(@PathVariable("id") String publishId) throws ClientException, IOException {
        // 往消息中间件中存数据 （type=5）
        quanZiMqService.sendMsg(publishId,5);
        return quanziService.removeLikePublish(publishId);
    }

    @GetMapping(value = "/movements/{id}/unlove",name = "删除喜欢")
    public ResponseEntity removeLovePublish(@PathVariable("id") String publishId) throws ClientException, IOException {
        // 往消息中间件中存数据 （type=6）
        quanZiMqService.sendMsg(publishId,6);
        return quanziService.removeLovePublish(publishId);
    }


    @GetMapping(value = "/movements/recommend",name = "我的推荐动态")
    public ResponseEntity findRecommendPublish(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) throws ClientException, IOException {
        return quanziService.findRecommendPublish(page,pagesize);
    }

    @GetMapping(value = "/comments/{id}/like",name = "圈子评论点赞")
    public ResponseEntity likeComments(@PathVariable String id){
        return null;
    }
}
